package com.znufe.exam.third.votesdk.impl;

import com.znufe.exam.base.BaseConstant;
import com.znufe.exam.domain.AttendCourseRecord;
import com.znufe.exam.domain.CheckIn;
import com.znufe.exam.domain.vote.SignIn;
import com.znufe.exam.service.AttendCourseRecordService;
import com.znufe.exam.service.CheckInService;
import com.znufe.exam.service.SignInService;
import com.znufe.exam.service.VoteConfigInfoService;
import com.znufe.exam.service.impl.SignInServiceImpl;
import com.znufe.exam.third.votesdk.JSigninCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName:
 * @Package: com.znufe.exam.third.votesdkController
 * @ClassName: JSignin
 * @Author: bot
 * @Description:
 * @Date: 2020/6/15 16:48
 * @Version: 1.0
 */
@Component
public class JOnSignin implements JSigninCallback {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private static JOnSignin jOnSignin;

    @Autowired
    public SignInService signInService;
    @Autowired
    public CheckInService checkInService;
    @Autowired
    public AttendCourseRecordService attendCourseRecordService;

    @PostConstruct
    public void init() {
        jOnSignin = this;
        jOnSignin.signInService = this.signInService;
        jOnSignin.checkInService = this.checkInService;
        jOnSignin.attendCourseRecordService = this.attendCourseRecordService;
    }

    /**
     * 新业务流程：老师点击开始签到，会将课程ID、班级ID、教师ID插入到attend_course_record表，
     * 签到服务会查询attend_course_record表，找到最后一条签到记录，默认为当前上课签到班级；
     * 获取课程ID、班级ID、答题器ID、基站ID后写入到check_in表；
     * <p>
     * * @param BasementID 答题器基站ID
     *
     * @param KeyID   答题器子键ID
     * @param KeyTime 答题持续时间（不存入数据库）
     * @return
     */
    @Override
    public int invoke(short BasementID, short KeyID, float KeyTime) {
        logger.info("JSignin invoke is begin ");
        logger.info("BasementID is:" + BasementID + "; KeyID is:" + KeyID + "; KeyTime is:" + KeyTime);
        //从attend_course_record表找到最后一条签到记录
        AttendCourseRecord attendCourseRecord = jOnSignin.attendCourseRecordService.selectLastRecord();

        if (attendCourseRecord != null ) {
            CheckIn checkIn = new CheckIn();
            checkIn.setVoteId((int) KeyID);
            checkIn.setVoteBaseId((int) BasementID);
            checkIn.setClassId(attendCourseRecord.getClassId());
            checkIn.setCourseId(attendCourseRecord.getCourseId());
            checkIn.setIsCheckIn(BaseConstant.SUCCESS);
            checkIn.setModifyTime(new Date());
            jOnSignin.checkInService.insertSelective(checkIn);
            logger.info("通过答题器插入签到信息成功:" + checkIn.toString());
        } else {
            logger.error("未查询到上课记录信息");
        }
        logger.info("JSignin invoke is end ");
        return 0;
    }

    /**
     *   原有签到业务流程：上课前教师导入学生名单，为学生分配好答题器子键（唯一），学生通过答题器子键进行签到
     *      *    签到时，根据答题器子键、基站ID获取到学生签到信息，置签到状态为1已签到
     * @param BasementID
     * @param KeyID
     * @param KeyTime
     * @return

     @Override public int invoke(short BasementID, short KeyID, float KeyTime) {
     logger.info("JSignin invoke is begin ");
     logger.info("BasementID is:" + BasementID + "; KeyID is:" + KeyID + "; KeyTime is:" + KeyTime);


     SignIn signIn = new SignIn();
     signIn.setVoteId(String.valueOf(KeyID));
     signIn.setVoteBaseId(String.valueOf(BasementID));
     List<SignIn> signInList = jOnSignin.signInService.querySignInList(signIn);
     //实际情况中可能会存在多条记录
     if(signInList != null && signInList.size() > 0){
     signIn = signInList.get(0);
     //只取最后一条设置为已签到
     signIn.setIsSignIn(BaseConstant.SUCCESS);
     signIn.setModifyTime(new Date());
     jOnSignin.signInService.updateByPrimaryKeySelective(signIn);
     logger.info("通过答题器插入学生签到信息成功:" + signIn.toString());
     }else{
     logger.error("未查询到对应子键的学生信息，子键ID为：" + KeyID);
     }
     logger.info("JSignin invoke is end ");
     return 0;
     }
     */
}
